﻿
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudStorageLight.DesktopTool
{
    public class WordImageConverter : DocumentImageConverter
    {
        public WordImageConverter()
        {
        }

        public override bool ToXpsFile(string inputFile, string outputPatternFile, int pageSize)
        {
            var wordApplication = new Microsoft.Office.Interop.Word.Application();
            
            Microsoft.Office.Interop.Word.Document docDocument = null;
            var xpsfile = GetExtnsionFilename(outputPatternFile, "xps");
            var outfile = GetExtnsionFilename(outputPatternFile, "jpg");
            try
            {
                docDocument = wordApplication.Documents.Open(inputFile);
                docDocument.ExportAsFixedFormat(
                    GetExtnsionFilename(outputPatternFile, "xps"),
                    WdExportFormat.wdExportFormatXPS, false,
                    WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportFromTo, 1, pageSize);
                return true;
            }
            finally
            {
                if (docDocument != null)
                {
                    object opt1 = WdSaveOptions.wdDoNotSaveChanges;
                    docDocument.Close(ref opt1);
                    docDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit();
                    wordApplication = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

        }
    }
}
